﻿
Public Class ajaxresponse
    Public Const functionName As String = "ajaxrsp"
    Public Const requestName As String = "xr"
    Public Const defaultFailedToGetResponse As String = "'failed to get response from server.'"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        Dim content As String = Nothing
        'r = url, u = user, p = password, b = body, f = failed text
        content = "function " + functionName + "(r,u,p,b,f){"
        content += var.invoke(requestName, xmlrequest.invoke(inlineScript)) + ";"
        Dim succAction As String = Nothing
        succAction += requestName + ".open('POST'," + htmlDecode.invoke(inlineScript, "r") + ",false,u,p);"
        succAction += trycatch.invoke(requestName + ".timeout=10000")
        succAction += ifelse.invoke(notnull.invoke(inlineScript, "b"), _
                                    requestName + ".setRequestHeader('Content-Type'," + _
                                                  "'" + network.http.contentType.www_form_urlencoded + "')")
        succAction += requestName + ".send(b);"
        succAction += ifelse.invoke(requestName + ".readyState==4 && " + requestName + ".status==200", _
                                    "return " + requestName + ".responseText", _
                                    "return f")
        content += ifelse.invoke(null.invoke(inlineScript, requestName), "return f", _
                                 trycatch.invoke(succAction, "return f"))
        content += "}"
        inlineScript.addscript("ajaxresponse", content)
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal requestUrl As String, _
                                  Optional ByVal userName As String = Nothing, _
                                  Optional ByVal password As String = Nothing, _
                                  Optional ByVal body As String = Nothing, _
                                  Optional ByVal failedToGetResponse As String = defaultFailedToGetResponse, _
                                  Optional ByVal escapeUrl As Boolean = True, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        'assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        'safe to escape here,
        '1. always use ' to wrap a string
        '2. if it's a string var, the name should not be escaped
        actionTarget += functionName + "(" + _
                        ifoper(escapeUrl, htmlUtils.escape(requestUrl), requestUrl) + "," + _
                        ifoper(isemptystring(userName), null.name(inlineScript), userName) + "," + _
                        ifoper(isemptystring(password), null.name(inlineScript), password) + "," + _
                        ifoper(isemptystring(body), null.name(inlineScript), body) + "," + _
                        ifoper(isemptystring(failedToGetResponse), defaultFailedToGetResponse, failedToGetResponse) + _
                        ")"
        Return actionTarget
    End Function

    Public Shared Function invoke(ByVal page As xhtmlpage, ByVal requestUrl As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        assert(Not page Is Nothing, "page is nothing.")
        Return invoke(page.inlineScript(), requestUrl, actionTarget)
    End Function
End Class

Public Class ajaxcall
    Public Const functionName As String = "ajaxcall"
    Public Const requestName As String = "xr"
    Public Const defaultFailedToGetResponse As String = "'failed to get response from server.'"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        Dim content As String = Nothing
        'r = url, u = user, p = password, b = body, c = callback function, f = failed text
        content = "function " + functionName + "(r,u,p,b,c,f){"
        content += var.invoke(requestName, xmlrequest.invoke(inlineScript)) + ";"
        Dim succAction As String = Nothing
        succAction += requestName + ".open('POST'," + htmlDecode.invoke(inlineScript, "r") + "," + _
                                           [true].name(inlineScript) + ",u,p);"
        succAction += trycatch.invoke(requestName + ".timeout=10000")
        succAction += ifelse.invoke(notnull.invoke(inlineScript, "b"), _
                                    requestName + ".setRequestHeader('Content-Type'," + _
                                                  "'" + network.http.contentType.www_form_urlencoded + "')")
        succAction += requestName + ".send(b);"
        succAction += requestName + ".onreadystatechange=function(){"
        succAction += ifelse.invoke(requestName + ".readyState==4 && " + requestName + ".status==200", _
                                    "c(" + requestName + ".responseText)", _
                                    "c(f)") + "};"
        content += ifelse.invoke(null.invoke(inlineScript, requestName), "c(f)", _
                                 trycatch.invoke(succAction, "c(f)"))
        content += "}"
        inlineScript.addscript("ajaxcall", content)
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal requestUrl As String, _
                                  Optional ByVal userName As String = Nothing, _
                                  Optional ByVal password As String = Nothing, _
                                  Optional ByVal body As String = Nothing, _
                                  Optional ByVal callback As String = Nothing, _
                                  Optional ByVal failedToGetResponse As String = defaultFailedToGetResponse, _
                                  Optional ByVal escapeUrl As Boolean = True, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        'assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        'safe to escape here,
        '1. always use ' to wrap a string
        '2. if it's a string var, the name should not be escaped
        actionTarget += functionName + "(" + _
                        ifoper(escapeUrl, requestUrl, requestUrl) + "," + _
                        ifoper(isemptystring(userName), null.name(inlineScript), userName) + "," + _
                        ifoper(isemptystring(password), null.name(inlineScript), password) + "," + _
                        ifoper(isemptystring(body), null.name(inlineScript), body) + "," + _
                        ifoper(isemptystring(callback), null.name(inlineScript), callback) + "," + _
                        ifoper(isemptystring(failedToGetResponse), defaultFailedToGetResponse, failedToGetResponse) + _
                        ")"
        Return actionTarget
    End Function

    Public Shared Function invoke(ByVal page As xhtmlpage, ByVal requestUrl As String, ByVal callback As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        assert(Not page Is Nothing, "page is nothing.")
        Return invoke(page.inlineScript(), requestUrl, callback, actionTarget)
    End Function
End Class

Public Class xmlrequest
    Public Const functionName As String = "crtxmlreq"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        null.register(inlineScript)
#If RETIRED Then
        inlineScript.addscript("xmlrequest", "function " + functionName + "(){" + _
                               ifelse.invoke("XMLHttpRequest", "return new XMLHttpRequest()", _
                                              ifelse.invoke("window.ActiveXObject", _
                                                            "return new ActiveXObject('Microsoft.XMLHTTP')", _
                                                            "return " + null.name)) + _
                               "}")
#End If
        inlineScript.addscript("xmlrequest", "function " + functionName + "(){" + _
                               "return (" + _
                                   questioncolon.invoke(window.name(inlineScript) + ".XMLHttpRequest", _
                                                        "new " + window.name(inlineScript) + ".XMLHttpRequest()", _
                                                        "(" + questioncolon.invoke( _
                                                            window.name(inlineScript) + ".ActiveXObject", _
                                                            "new " + window.name(inlineScript) + _
                                                                     ".ActiveXObject('Microsoft.XMLHTTP')", _
                                                            null.name(inlineScript)) + "));}"))
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If
        actionTarget += functionName + "()"
        Return actionTarget
    End Function

    Public Shared Function invoke(ByVal page As xhtmlpage, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        assert(Not page Is Nothing, "page is nothing.")
        Return invoke(page.inlineScript(), actionTarget)
    End Function
End Class

Public Class changeInnerHtmlByIdWithAjaxCall
    Public Shared Sub register(ByVal inlineScript As inlineScript)
        'assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        changeInnerHtmlById.register(inlineScript)
        ajaxresponse.register(inlineScript)
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal url As String, ByVal id As String, _
                                  Optional ByRef actionTarget As String = Nothing, _
                                  Optional ByVal failedToGetResponseText As String = _
                                      ajaxresponse.defaultFailedToGetResponse) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        actionTarget += changeInnerHtmlById.invoke(inlineScript, id, _
                                                   ajaxresponse.invoke(inlineScript, url, _
                                                                       failedToGetResponse:=failedToGetResponseText))
        Return actionTarget
    End Function
End Class

Public Class changeInnerHtmlAndShowByIdWithAjaxCall
    Public Const functionName As String = "cihsidajax"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        changeInnerHtmlByIdWithAjaxCall.register(inlineScript)
        displayById.register(inlineScript)

        'u = url, i = id, f = failed text
        inlineScript.addscript("changeInnerHtmlAndShowByIdWithAjaxCall", "function " + functionName + "(u,i,f){" + _
                               changeInnerHtmlByIdWithAjaxCall.invoke(inlineScript, "u", "i", _
                                                                      failedToGetResponseText:="f") _
                               + ";" + _
                               showById.invoke(inlineScript, "i") + ";}")
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal url As String, ByVal id As String, _
                                  Optional ByRef actionTarget As String = Nothing, _
                                  Optional ByVal failedToGetResponseText As String = _
                                      ajaxresponse.defaultFailedToGetResponse) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        actionTarget += functionName + "(" + url + "," + id + "," + _
                        ifoper(isemptystring(failedToGetResponseText), _
                               ajaxresponse.defaultFailedToGetResponse, failedToGetResponseText) + ")"
        Return actionTarget
    End Function
End Class